package org.keyroy.mapEditor.ui.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.keyroy.map.core.Map;
import org.keyroy.mapEditor.ui.beans.ProjectUtil;

public class MapParamSetDialog extends Shell {
	private Text name_text;
	private Label label_1;
	private Label label_2;
	private Spinner columns_text;
	private Spinner rows_text;
	private Spinner width_text;
	private Spinner height_text;

	// /**
	// * Launch the application.
	// *
	// * @param args
	// */
	// public static void main(String args[]) {
	// try {
	// Display display = Display.getDefault();
	// MapParamSetView shell = new MapParamSetView(display, null);
	// shell.open();
	// shell.layout();
	// while (!shell.isDisposed()) {
	// if (!display.readAndDispatch()) {
	// display.sleep();
	// }
	// }
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }

	/**
	 * Create the shell.
	 * 
	 * @param display
	 * @wbp.parser.constructor
	 */
	public MapParamSetDialog(Display display, final OnMapSetListener mapSetListener) {
		super(display, SWT.CLOSE | SWT.TITLE);
		setSize(275, 149);
	
		setImage(SWTResourceManager.getImage(MapParamSetDialog.class, "/res/icons/overview_obj.gif"));
		setLayout(new GridLayout(4, true));

		Label label = new Label(this, SWT.NONE);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		label.setText("\u540D\u79F0 : ");

		name_text = new Text(this, SWT.BORDER);
		name_text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 3, 1));

		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		lblNewLabel.setText("\u884C\u5217 : ");

		columns_text = new Spinner(this, SWT.BORDER);
		columns_text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		columns_text.setMaximum(65535);
		columns_text.setMinimum(1);
		columns_text.setSelection(20);

		Label lblX = new Label(this, SWT.NONE);
		lblX.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		lblX.setAlignment(SWT.CENTER);
		lblX.setText("X");

		rows_text = new Spinner(this, SWT.BORDER);
		rows_text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		rows_text.setMaximum(65535);
		rows_text.setMinimum(1);
		rows_text.setSelection(20);

		label_1 = new Label(this, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		label_1.setText("\u5BBD\u9AD8 : ");

		width_text = new Spinner(this, SWT.BORDER);
		width_text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		width_text.setMaximum(65535);
		width_text.setMinimum(1);
		width_text.setSelection(40);

		label_2 = new Label(this, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		label_2.setAlignment(SWT.CENTER);
		label_2.setText("X");

		height_text = new Spinner(this, SWT.BORDER);
		height_text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		height_text.setMaximum(65535);
		height_text.setMinimum(1);
		height_text.setSelection(40);
		new Label(this, SWT.NONE);
						
								Button btnNewButton = new Button(this, SWT.NONE);
								btnNewButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 3, 1));
								btnNewButton.addSelectionListener(new SelectionAdapter() {
									@Override
									public void widgetSelected(SelectionEvent e) {
										if (mapSetListener != null) {
											String name = name_text.getText();
											int columns = Integer.parseInt(columns_text.getText());
											int rows = Integer.parseInt(rows_text.getText());
											int gWidth = Integer.parseInt(width_text.getText());
											int gHeight = Integer.parseInt(height_text.getText());
											mapSetListener.onMapSet(name, columns, rows, gWidth, gHeight);
										}
										dispose();
									}

								});
								btnNewButton.setText("\u5B8C\u6210");
		createContents();
	}

	public MapParamSetDialog(Display display, Map map, final OnMapSetListener mapSetListener) {
		this(display, mapSetListener);
		name_text.setText(map.getName());
		columns_text.setSelection(map.getColumns());
		rows_text.setSelection(map.getRows());
		width_text.setSelection(map.getGridWidth());
		height_text.setSelection(map.getGridHeight());

		name_text.setSelection(name_text.getText().length());
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("\u5730\u56FE\u53C2\u6570\u8BBE\u7F6E");
		name_text.setText(ProjectUtil.findFreeMapName());
		name_text.setSelection(name_text.getText().length());
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public static interface OnMapSetListener {
		public void onMapSet(String name, int columns, int rows, int gWidth, int gHeight);
	}
}
